package com.solome.map;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;

/**
 * @author soells
 * @E-mail:soells@foxmail.com
 * @version 创建时间：2014年9月15日 下午4:24:06
 * @DES 程序的简单说明
 */
public class LoadMap {
	class MapData {
		public byte type;// 有障碍物
		public int mType;// 有障碍物
		public byte flip;
		public int fType;// 有障碍物
		public int clipX, clipY;
		public byte tempByte;
		public short x, y;
	}

	class MapInfo {
		int tempName;
		int tempX;
		int tempY;
	}

	String extRoot = Gdx.files.getExternalStoragePath();
	String locRoot = Gdx.files.getLocalStoragePath();

	// MapData[][] roadData;
	public List<MapData> roadData = new CopyOnWriteArrayList<MapData>();//

	// public List<MapData> roadData = new CopyOnWriteArrayList<MapData>();//
	/********** 处理装饰物层(中间层) ********/
	public List<MapInfo> map1Dicts = new CopyOnWriteArrayList<MapInfo>();//
	/********** 处理装饰物层(遍历层) ********/
	public List<MapInfo> map2Dicts = new CopyOnWriteArrayList<MapInfo>();//
	/********** 处理装饰物层(天空层) ********/
	public List<MapInfo> map3Dicts = new CopyOnWriteArrayList<MapInfo>();//
	/********** 处理装饰物层(动态层) ********/
	public List<MapInfo> map4Dicts = new CopyOnWriteArrayList<MapInfo>();//

	int colNum, rowNum, colW, colH, mapW, mapH;
	byte backID;
	short tileID;

	public boolean openMapData(String path) {

		ByteArrayInputStream bais = null;
		DataInputStream rfile = null;
		try {
			FileHandle handle = Gdx.files.internal(path);
			byte[] iData = handle.readBytes();
			bais = new ByteArrayInputStream(iData);
			rfile = new DataInputStream(bais);

			// TODO
			/******** 地面层信息 **********/
			colNum = rfile.readByte() & 0xff; // 列数
			rowNum = rfile.readByte() & 0xff; // 行数
			// 新版本,第二个字节表示版本数
			colW = rfile.readByte() & 0xff;// 大小
			colH = colW;

			mapW = colNum * colW;
			mapH = rowNum * colH;
			//
			// TiledMap map = new TiledMap();
			// MapProperties mapProperties = map.getProperties();
			// mapProperties.put("width", mapW);
			// mapProperties.put("height", mapH);
			// mapProperties.put("tilewidth", colW);
			// mapProperties.put("tileheight", colH);

			backID = (byte) (rfile.readByte() & 0xff);//
			tileID = (short) (rfile.readShort() & 0xffff); // 切片索引ID
			// roadData = new MapData[colNum][rowNum];
			for (int i = 0; i < colNum; i++) {// 列
				for (int j = 0; j < rowNum; j++) {// 行
					// 先读取一个基本的信息
					int mapDataInfo = rfile.readUnsignedByte();
					// 取出第1位判断是否有障碍物
					short ifBlock = (short) (mapDataInfo >> 5);
					MapData mapData1 = new MapData();
					if (ifBlock != 0) { // 有障碍物
						mapData1.type = (byte) ifBlock;
					}
					byte ifMove = (byte) ((mapDataInfo >> 4) & 0x01);
					if (ifMove != 0) { // 有障碍物
						mapData1.mType = 1;
					}
					// 取出第2到4位表示翻转
					byte flip = (byte) (mapDataInfo >> 1);
					mapData1.flip = (byte) (flip & 0x07);
					byte ifFR = (byte) (mapDataInfo & 0x01);
					if (ifFR != 0) { // 有障碍物
						mapData1.fType = 1;
					}
					// 开始赋值
					byte tempByte = rfile.readByte(); // 第一层图片索引

					mapData1.tempByte = tempByte;
					if (tempByte != -1) { // 如果不是-1
						// 修改超过128个图素条
						int tempByte_int = (tempByte & 0xFF);
						mapData1.clipX = colW * tempByte_int;
					}
					// roadData[i][j] = mapData1;
					roadData.add(mapData1);
				}
			}

			// 0:地图元素 1:障碍 2:NPC 3:传送门
			// npc部分
			// short npcCount = rfile.readShort();
			// for (int i = 0; i < npcCount; i++) {
			// byte tempX = rfile.readByte();
			// byte tempY = rfile.readByte();
			// short tempData = rfile.readShort();
			// if (mainJPanel.roadData1[tempX][tempY] != null) {
			// MapData mapData = (MapData) mainJPanel.roadData1[tempX][tempY];
			// mapData.type = 2;
			// mapData.data = tempData;
			// }
			// }
			// // 传送们
			// short doorCount = rfile.readShort();
			// for (int i = 0; i < doorCount; i++) {
			// byte tempX = rfile.readByte();
			// byte tempY = rfile.readByte();
			// short tempData = rfile.readShort();
			// if (mainJPanel.roadData1[tempX][tempY] != null) {
			// MapData mapData = (MapData) mainJPanel.roadData1[tempX][tempY];
			// mapData.type = 3;
			// mapData.data = tempData;
			// }
			// }

			/** -------------加载物件层------------------- */
			byte adorningCount = rfile.readByte();
			int adorningCounttemp = adorningCount & 0xFF;
			for (int i = 0; i < adorningCounttemp; i++) {
				int tempName = 0xffff & rfile.readShort();
				int tempX = rfile.readShort();
				int tempY = rfile.readShort();
				MapInfo info = new MapInfo();
				info.tempName = tempName;
				info.tempX = tempX;
				info.tempY = tempY;
				map1Dicts.add(info);
			}
			/** -------------天空层物件------------------- */
			byte adorningCount2 = rfile.readByte();
			int adorningCount2temp = adorningCount2 & 0xFF;
			for (int i = 0; i < adorningCount2temp; i++) {
				int tempName = rfile.readShort() & 0xffff;
				int tempX = rfile.readShort();
				int tempY = rfile.readShort();
				MapInfo info = new MapInfo();
				info.tempName = tempName;
				info.tempX = tempX;
				info.tempY = tempY;
				map2Dicts.add(info);
			}
			/** -------------地表物件层------------------- */
			byte adorningCount3 = rfile.readByte();
			int adorningCount3temp = adorningCount3 & 0xFF;
			for (int i = 0; i < adorningCount3temp; i++) {
				int tempName = rfile.readShort() & 0xffff;
				int tempX = rfile.readShort();
				int tempY = rfile.readShort();
				MapInfo info = new MapInfo();
				info.tempName = tempName;
				info.tempX = tempX;
				info.tempY = tempY;
				map3Dicts.add(info);
			}

			/********** 处理装饰物层(动态层) ********/
			byte adorningCount4 = rfile.readByte();
			int adorningCount4temp = adorningCount4 & 0xFF;
			for (int i = 0; i < adorningCount4temp; i++) {
				int tempName = rfile.readShort() & 0xffff;
				int tempX = rfile.readShort();
				int tempY = rfile.readShort();
				MapInfo info = new MapInfo();
				info.tempName = tempName;
				info.tempX = tempX;
				info.tempY = tempY;
				map4Dicts.add(info);
			}
			return true;
		} catch (Exception ex) {
			// util.ImageUtil.logException(ex);
			System.out.println(ex);
		}
		return false;
	}

}
